package staffManager;

import Bean.CheckBean;
import Bean.SqlBean;
import loginRegister.LoginBean;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author 黄忠
 */
public class AddStaffServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String userName=new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
        String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String sex=new String(request.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
        String idCard=new String(request.getParameter("idCard").getBytes("ISO-8859-1"),"UTF-8");
        String nation=new String(request.getParameter("nation").getBytes("ISO-8859-1"),"UTF-8");
        String edu=new String(request.getParameter("edu").getBytes("ISO-8859-1"),"UTF-8");
        String work=new String(request.getParameter("work").getBytes("ISO-8859-1"),"UTF-8");
        String monthPay=new String(request.getParameter("monthPay").getBytes("ISO-8859-1"),"UTF-8");
        String level=new String(request.getParameter("level").getBytes("ISO-8859-1"),"UTF-8");

        HttpSession session=request.getSession();
        LoginBean login=(LoginBean)session.getAttribute("login");
        if(login==null){
            response.sendRedirect("http://localhost:8080/login.jsp");
        }
        else if(!CheckBean.is_numberOrAlpha(userName)){
            CheckBean.alert("账号由数字或字母组成!");
            response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
        }
        else if(!CheckBean.is_chineseOrAlpha(name)){
            CheckBean.alert("姓名由汉字或字母组成!");
            response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
        }
        else if(!CheckBean.is_numberOrAlpha(password)){
            CheckBean.alert("密码由数字或字母组成!");
            response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
        }
        else if(!CheckBean.isIdentityCode(idCard)){
            CheckBean.alert("身份证格式不正确!");
            response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
        }
        else if(monthPay.contains("-")){
            CheckBean.alert("工资不能为负数!");
            response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
        }
        else {
            String sql = "select * from user where userName='" + userName + "'";
            try {
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                rs = sb.executeQuery(sql);
                rs.last();
                if (rs.getRow() > 0) {
                    CheckBean.alert("账号已存在!");
                    response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
                }
                else {
                    String birth=idCard.substring(6,10)+"-"+idCard.substring(10,12)+"-"+idCard.substring(12,14);
                    sql = "insert into user" + "(userName,password,name,sex,idCard,birth,nation,edu,work,monthPay,level)" +
                            "values('" + userName + "','" + password + "','" + name + "','" + sex + "','" + idCard + "','" + birth + "','" + nation + "','" + edu + "','" + work + "','" + monthPay + "','" + level + "')";
                    sb.executeUpdate(sql);
                    sql = "select * from user";
                    rs = sb.executeQuery(sql);
                    ArrayList staffslist = null;
                    staffslist = new ArrayList();
                    while (rs.next()) {
                        if (!login.getUserName().equals(rs.getString("userName"))) {
                            StaffBean ff = new StaffBean(rs);
                            if(!ff.getLevel().equals("超级管理员"))
                                staffslist.add(ff);
                        }
                    }
                    session.setAttribute("staffslist", staffslist);
                    sb.closeSQL();
                    CheckBean.alert("添加成功!");
                    response.sendRedirect("http://localhost:8080/staffManager/addStaff.jsp");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        doGet(request, response);
    }

}
